home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 May / PCWMAY06.iso / Software / Freeware / First Page 2006 3.00 / fp2006-final-3.00-setup.exe / {app} / Iscripts / Forms Misc / auto-currency.izs < prev    next >
Text File  |  2005-09-27  |  4KB  |  160 lines

  1. <!NOWIZARD>
  2.  
  3. <!TITLE>Auto Currency 
  4. <!/TITLE>
  5.  
  6. <!DESCRIPTION>This script inserts the proper seperators to automatically format any currency field. <!/DESCRIPTION> 
  7.  
  8. <!CATEGORY>Forms<!/CATEGORY>
  9.  
  10. <!SCRIPT>
  11. <!-- START OF SCRIPT -->
  12.  
  13. <!-- HOW TO INSTALL AUTO CURRENCY:
  14.  
  15.   1.  Copy code into the HEAD section of document
  16.   2.  Put last coding into the BODY section of document  -->
  17.  
  18. <!-- STEP ONE: Add code into HEAD section of document  -->
  19.  
  20. <HEAD>
  21.  
  22. <SCRIPT LANGUAGE="JavaScript">
  23. <!-- Original:  Mario Costa (mariocosta@openlink.com.br) -->
  24.  
  25.  
  26. <!-- Begin
  27. function currencyFormat(fld, milSep, decSep, e) {
  28. var sep = 0;
  29. var key = '';
  30. var i = j = 0;
  31. var len = len2 = 0;
  32. var strCheck = '0123456789';
  33. var aux = aux2 = '';
  34. var whichCode = (window.Event) ? e.which : e.keyCode;
  35. if (whichCode == 13) return true;  // Enter
  36. key = String.fromCharCode(whichCode);  // Get key value from key code
  37. if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
  38. len = fld.value.length;
  39. for(i = 0; i < len; i++)
  40. if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
  41. aux = '';
  42. for(; i < len; i++)
  43. if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
  44. aux += key;
  45. len = aux.length;
  46. if (len == 0) fld.value = '';
  47. if (len == 1) fld.value = '0'+ decSep + '0' + aux;
  48. if (len == 2) fld.value = '0'+ decSep + aux;
  49. if (len > 2) {
  50. aux2 = '';
  51. for (j = 0, i = len - 3; i >= 0; i--) {
  52. if (j == 3) {
  53. aux2 += milSep;
  54. j = 0;
  55. }
  56. aux2 += aux.charAt(i);
  57. j++;
  58. }
  59. fld.value = '';
  60. len2 = aux2.length;
  61. for (i = len2 - 1; i >= 0; i--)
  62. fld.value += aux2.charAt(i);
  63. fld.value += decSep + aux.substr(len - 2, len);
  64. }
  65. return false;
  66. }
  67. //  End -->
  68. </script>
  69.  
  70. </HEAD>
  71.  
  72. <!-- STEP TWO: Add code into BODY section of document  -->
  73.  
  74. <BODY>
  75.  
  76. <form>
  77. Enter Value:  
  78. <input type=text name=test length=15 onKeyPress="return(currencyFormat(this,',','.',event))">
  79. </form>
  80.  
  81. <!-- END OF SCRIPT -->
  82. <!/SCRIPT>
  83.  
  84. <!PREVIEW>
  85. <!-- START OF SCRIPT -->
  86.  
  87. <!-- HOW TO INSTALL AUTO CURRENCY:
  88.  
  89.   1.  Copy code into the HEAD section of document
  90.   2.  Put last coding into the BODY section of document  -->
  91.  
  92. <!-- STEP ONE: Add code into HEAD section of document  -->
  93.  
  94. <HEAD>
  95.  
  96. <SCRIPT LANGUAGE="JavaScript">
  97. <!-- Original:  Mario Costa (mariocosta@openlink.com.br) -->
  98.  
  99.  
  100. <!-- Begin
  101. function currencyFormat(fld, milSep, decSep, e) {
  102. var sep = 0;
  103. var key = '';
  104. var i = j = 0;
  105. var len = len2 = 0;
  106. var strCheck = '0123456789';
  107. var aux = aux2 = '';
  108. var whichCode = (window.Event) ? e.which : e.keyCode;
  109. if (whichCode == 13) return true;  // Enter
  110. key = String.fromCharCode(whichCode);  // Get key value from key code
  111. if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
  112. len = fld.value.length;
  113. for(i = 0; i < len; i++)
  114. if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
  115. aux = '';
  116. for(; i < len; i++)
  117. if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
  118. aux += key;
  119. len = aux.length;
  120. if (len == 0) fld.value = '';
  121. if (len == 1) fld.value = '0'+ decSep + '0' + aux;
  122. if (len == 2) fld.value = '0'+ decSep + aux;
  123. if (len > 2) {
  124. aux2 = '';
  125. for (j = 0, i = len - 3; i >= 0; i--) {
  126. if (j == 3) {
  127. aux2 += milSep;
  128. j = 0;
  129. }
  130. aux2 += aux.charAt(i);
  131. j++;
  132. }
  133. fld.value = '';
  134. len2 = aux2.length;
  135. for (i = len2 - 1; i >= 0; i--)
  136. fld.value += aux2.charAt(i);
  137. fld.value += decSep + aux.substr(len - 2, len);
  138. }
  139. return false;
  140. }
  141. //  End -->
  142. </script>
  143.  
  144. </HEAD>
  145.  
  146. <!-- STEP TWO: Add code into BODY section of document  -->
  147.  
  148. <BODY>
  149.  
  150. <form>
  151. Enter Value:  
  152. <input type=text name=test length=15 onKeyPress="return(currencyFormat(this,',','.',event))">
  153. </form>
  154.  
  155.  
  156. <!-- END OF SCRIPT -->
  157. <!/PREVIEW>
  158.  
  159. <!RELATED>NONE<!/RELATED>
  160.